package cn.manyufun.myf.ui.main.fragment;

import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;

import butterknife.BindView;
import butterknife.OnClick;
import cn.manyufun.myf.R;
import cn.manyufun.myf.bean.ComicsListBean;
import cn.manyufun.myf.common.base.BaseFragment;
import cn.manyufun.myf.common.commonwidget.LoadingTip;
import cn.manyufun.myf.common.irecyclerview.IRecyclerView;
import cn.manyufun.myf.common.irecyclerview.OnRefreshListener;
import cn.manyufun.myf.common.irecyclerview.widget.RecycleViewDivider;
import cn.manyufun.myf.ui.comics.adapter.ComicsListAdapter;
import cn.manyufun.myf.ui.comics.contract.ComicsListContract;
import cn.manyufun.myf.ui.main.activity.SearchResultActivity;
import cn.manyufun.myf.ui.main.model.ComicsListModel;
import cn.manyufun.myf.ui.main.presenter.ComicsListPresenter;

/**
 * @author Kevin Xiao
 * @date Create Date: 2017-12-26
 * @Description
 */
public class ComicsFragment extends BaseFragment<ComicsListPresenter, ComicsListModel> implements ComicsListContract.View, OnRefreshListener{

    @BindView(R.id.irc)
    IRecyclerView irc;
    @BindView(R.id.loadedTip)
    LoadingTip loadedTip;

    @OnClick(R.id.toolbar_ic_search)
    public void clickToSearch() {
        SearchResultActivity.startAction(getContext());
    }

    private ComicsListBean datas = new ComicsListBean();
    private ComicsListAdapter comicsListAdapter;

    @Override
    public void showLoading(String title) {
    }

    @Override
    public void stopLoading() {
    }

    @Override
    public void showErrorTip(String msg) {
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.app_bar_comics;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        irc.setLayoutManager(new LinearLayoutManager(getContext()));
        datas = new ComicsListBean();
        comicsListAdapter = new ComicsListAdapter(getContext(), datas);
//        comicsListAdapter.openLoadAnimation(new ScaleInAnimation());
        irc.addItemDecoration(new RecycleViewDivider(getContext(), DividerItemDecoration.HORIZONTAL,
                10, getResources().getColor(R.color.container_background)));
        irc.setAdapter(comicsListAdapter);
        irc.setOnRefreshListener(this);
        //数据为空才重新发起请求
//        if (comicsListAdapter.getSize() <= 0) {
            mPresenter.getComicsListDataRequest();
//        }
    }


    @Override
    public void onRefresh() {
        irc.setRefreshing(true);
        mPresenter.getComicsListDataRequest();
    }


    @Override
    public void returnComicsListData(ComicsListBean comicsListBean) {
        irc.setRefreshing(false);
        comicsListAdapter.replaceAll(comicsListBean);
    }
}
